/***************************************************************************************************
Figure 8: Financial transactions and welfare gains by wealth percentile
a) Housing and equity transactions
b) Debt and deposits
***************************************************************************************************/

import delimited "$MyPath/Data/Micro Data/year_cohort_percentile", clear
rename *_sum *
merge m:1 year using "$MyPath/Save/kappa", keep(master matched) nogen
gen_transactions
replace debt_total = - debt_total
gen percentile_alt = round(percentile / 3) * 3
replace percentile_alt = percentile + 1 if percentile == 99


* sum across b_year but average across years (so that initial total_wealth corresponds to the right denominator)
collapse (sum) nt_housing_total nt_equity_total debt_total deposits_total total_wealth num_mean, by(percentile_alt year)
collapse (mean) nt_housing_total nt_equity_total debt_total deposits_total num_mean (first) total_wealth, by(percentile_alt)

* get scaled version
foreach v of varlist *_total {
	gen `v'_sc = `v' / total_wealth
}

twoway ///
(line nt_housing_total_sc percentile_alt if percentile_alt >= 4, lwidth(thick) color("$blue") lpattern(dash)) ///
(line nt_equity_total_sc percentile_alt if percentile_alt >= 4, lwidth(thick) color("$green") lpattern(longdash_dot)) ///
, ysize(4) xsize(6) graphregion(color(white) margin(l=0)) ///
yline(0, lpattern(solid) lwidth(thin) lcolor(black)) ///
xlabel(0 "p0" 20 "p20" 40 "p40" 60 "p60" 80 "p80" 100 "p100", labsize(medlarge)  grid glcolor(gs14) glwidth(vthin))  ///
ylabel(-0.015 "-1.5%" -0.01 "-1%" -0.005 "-0.5%" 0 "0%" 0.005 "0.5%", labsize(medlarge) grid glcolor(gs14) glwidth(vthin) angle(0)) ///
yscale(range(-0.015 0.005)) ///
ytitle("Net purchase", size(medlarge))  ///
xtitle("Initial percentile of total wealth (within cohorts)", size(medlarge)) xscale(titlegap(2)) ///
legend(order(1 "Housing " 2 "Equity" ) size(medlarge) rows(1) symysize(6) symxsize(8))
graph export "$MyPath/Figures/Figure8a.$suffix", replace

twoway ///
(line debt_total_sc percentile_alt if percentile_alt >= 4 , lwidth(thick) color("$red") lpattern(shortdash)) ///
(line deposits_total_sc percentile_alt if percentile_alt >= 4 , lwidth(thick) color("$yellow") lpattern(longdash)) ///
, ysize(4) xsize(6) graphregion(color(white) margin(l=0)) ///
yline(0, lpattern(solid) lwidth(thin) lcolor(black)) ///
xlabel(0 "p0" 20 "p20" 40 "p40" 60 "p60" 80 "p80" 100 "p100", labsize(medlarge)  grid glcolor(gs14) glwidth(vthin))  ///
ylabel(-0.15 "-15%" -0.1 "-10%" -0.05 "-5%" 0 "0%" 0.05 "5%", labsize(medlarge) grid glcolor(gs14) glwidth(vthin) angle(0)) ///
yscale(range(-0.15 0.05)) ///
ytitle("Holdings", size(medlarge))  ///
xtitle("Initial percentile of total wealth (within cohorts)", size(medlarge)) xscale(titlegap(2)) ///
legend(order(1 "Debt " 2 "Deposits" ) size(medlarge) rows(1) symysize(6) symxsize(8))
graph export "$MyPath/Figures/Figure8b.$suffix", replace

/***************************************************************************************************
Figure 8: Financial transactions and welfare gains by wealth percentile
c) Welfare gains
d) Welfare gains as a percent of total wealth
***************************************************************************************************/
import delimited "$MyPath/Data/Micro Data/year_cohort_percentile", clear
rename *_sum *
merge m:1 year using "$MyPath/Save/kappa", keep(master matched) nogen
gen_transactions
merge m:1 year using "$MyPath/Save/prices", keep(master matched) nogen
egen g = group(percentile b_year)
tsset g year
gen total_wealth_l = L.total_wealth
gen num_mean_l = L.num_mean
gen_welfare, by(percentile b_year)

* Collapse at rougher percentiles
gen percentile_alt = round(percentile / 3) * 3
replace percentile_alt = percentile + 1 if percentile == 99
collapse (sum) welfare* revgain* num_mean_l total_wealth_l, by(percentile_alt)

* do per capita and scaled version
replace total_wealth_l = total_wealth_l / num_mean_l
foreach v of varlist welfare* revgain*  {
	replace `v' = `v' / num_mean_l
	gen `v'_sc = `v' / total_wealth_l
}

* welfare by asset class
twoway ///
(line welfare_total percentile if percentile_alt >= 4, lwidth(thick) color(black)) ///
(line welfare_housing percentile if percentile_alt >= 4, lwidth(thick) color("$blue") lpattern(dash)) ///
(line welfare_debt percentile if percentile_alt >= 4, lwidth(thick) color("$red") lpattern(shortdash)) ///
(line welfare_deposits percentile if percentile_alt >= 4, lwidth(thick) color("$yellow") lpattern(longdash)) ///
(line welfare_equity percentile if percentile_alt >= 4, lwidth(thick)  color("$green") lpattern(longdash_dot)) ///
, yline(0, lpattern(solid) lwidth(thin) lcolor(black)) ///
ysize(4) xsize(6) graphregion(color(white) margin(l=0)) ///
xlabel(0 "p0" 20 "p20" 40 "p40" 60 "p60" 80 "p80" 100 "p100", labsize(medlarge)  grid glcolor(gs14) glwidth(vthin))  ///
ylabel(-25 "-25K" 0 "0K" 25 "25K"  50 "50K" 75 "75K" , labsize(medlarge) grid glcolor(gs14) glwidth(vthin) angle(0)) ///
xtitle("Initial percentile of total wealth (within cohorts)", size(medlarge)) xscale(titlegap(2)) ///
ytitle("Welfare gain", size(medlarge)) ///   
legend(order(1 "Total" 2 "Housing" 3 "Debt" 4 "Deposits" 5 "Equity") size(medlarge) rows(1) symysize(6) symxsize(8) region(lwidth(vvthin)))
graph export "$MyPath/Figures/Figure8c.$suffix", replace

* scaled welfare per asset class
twoway ///
(line welfare_total_sc percentile if percentile_alt >= 4, lwidth(thick) color(black)) ///
(line welfare_housing_sc percentile if percentile_alt >= 4, lwidth(thick) color("$blue") lpattern(dash)) ///
(line welfare_debt_sc percentile if percentile_alt >= 4, lwidth(thick) color("$red") lpattern(shortdash)) ///
(line welfare_deposits_sc percentile if percentile_alt >= 4, lwidth(thick) color("$yellow") lpattern(longdash)) ///
(line welfare_equity_sc percentile if percentile_alt >= 4, lwidth(thick)  color("$green") lpattern(longdash_dot)) ///
, yline(0, lpattern(solid) lwidth(thin) lcolor(black)) ///
ysize(4) xsize(6) graphregion(color(white) margin(l=0))  ///
xlabel(0 "p0" 20 "p20" 40 "p40" 60 "p60" 80 "p80" 100 "p100", labsize(medlarge)  grid glcolor(gs14) glwidth(vthin))  ///
xtitle("Initial percentile of total wealth (within cohorts)", size(medlarge)) xscale(titlegap(2)) ///
ylabel(-0.025 "-2.5%" 0 "0%" 0.025 "2.5%" 0.05 "5%", labsize(medlarge) grid glcolor(gs14) glwidth(vthin) angle(0)) ///
ytitle("Normalized welfare gain", size(medlarge)) ///
legend(order(1 "Total" 2 "Housing" 3 "Debt" 4 "Deposits" 5 "Equity") size(medlarge) rows(1) symysize(6) symxsize(8) region(lwidth(vvthin)))
graph export "$MyPath/Figures/Figure8d.$suffix", replace

* welfare versus revaluation gains
twoway ///
(line welfare_total percentile, color(black) lwidth(thick)) ///
(line revgain_total percentile, lwidth(thick) color(black%40) lpattern(dash)) ///
, ysize(4) xsize(6) graphregion(color(white) margin(l=0)) ///
yline(0, lpattern(solid) lwidth(thin) lcolor(black)) ///
xlabel(0 "p0" 20 "p20" 40 "p40" 60 "p60" 80 "p80" 100 "p100", labsize(medlarge)  grid glcolor(gs14) glwidth(vthin))  ///
ylabel(0 "0K" 250 "250K" 500 "500K"   750 "750K" 1000 "1000K", labsize(medlarge) grid glcolor(gs14) glwidth(vthin) angle(0)) ///
ytitle("", size(medlarge))  ///
xtitle("Initial percentile of total wealth (within cohorts)", size(medlarge)) xscale(titlegap(2)) ///
legend(order(1 "Welfare gains" 2 "Revaluation gains") size(medlarge) rows(1) symysize(6) symxsize(8) region(lwidth(vvthin)))
graph export "$MyPath/Figures/Figure9a.$suffix", replace

* welfare versus revaluation gains (scaled)
twoway ///
(line welfare_total_sc percentile, color(black) lwidth(thick)) ///
(line revgain_total_sc percentile, lwidth(thick) color(black%40) lpattern(dash)) ///
, ysize(4) xsize(6) graphregion(color(white) margin(l=0)) ///
yline(0, lpattern(solid) lwidth(thin) lcolor(black)) ///
xlabel(0 "p0" 20 "p20" 40 "p40" 60 "p60" 80 "p80" 100 "p100", labsize(medlarge)  grid glcolor(gs14) glwidth(vthin))  ///
ylabel(0 "0%" 0.2 "20%" 0.4 "40%" 0.6 "60%", labsize(medlarge) grid glcolor(gs14) glwidth(vthin) angle(0)) ///
yscale(range(0 0.6)) ///
ytitle("", size(medlarge) angle(90))  ///
xtitle("Initial percentile of total wealth (within cohorts)", size(medlarge)) xscale(titlegap(2)) ///
legend(order(1 "Welfare gains" 2 "Revaluation gains") size(medlarge) rows(1) symysize(6) symxsize(8) region(lwidth(vvthin)))
graph export "$MyPath/Figures/Figure9b.$suffix", replace

* Numbers used in Introduction as well as in Section 3.3 "Redistribution across wealth percentiles"
tempname handle
file open `handle' using "$MyPath/Numbers/wf_perc_100.tex", write replace
sum welfare_total if percentile == 100
file write `handle' "\\$`: di  %9.0fc r(mean)',000"
file close `handle'

tempname handle
file open `handle' using "$MyPath/Numbers/wf_sc_perc_100.tex", write replace
sum welfare_total_sc if percentile == 100
file write `handle' "`: di  %9.1fc 100 * r(mean)'"
file close `handle'

tempname handle
file open `handle' using "$MyPath/Numbers/wealth_sc_perc_100.tex", write replace
sum revgain_total_sc if percentile == 100
file write `handle' "`: di  %9.1fc 100 * r(mean)'"
file close `handle'


tempname handle
file open `handle' using "$MyPath/Numbers/wf_perc_1.tex", write replace
sum percentile
sum welfare_total if percentile == r(min)
file write `handle' "\\$`: di  %9.0fc r(mean)',000"
file close `handle'

tempname handle
file open `handle' using "$MyPath/Numbers/wf_sc_perc_2080.tex", write replace
sum welfare_total_sc if inrange(percentile, 20, 80)
file write `handle' "`: di  %9.1fc 100 * r(mean)'"
file close `handle'